package org.wang.generalcode.modules.sys.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.sys.constant.RedisKeyConstants;
import org.wang.generalcode.modules.sys.pojo.entity.Config;
import org.wang.generalcode.modules.sys.pojo.params.ConfigPageParams;
import org.wang.generalcode.modules.sys.service.IConfigService;
import org.wang.generalcode.modules.sys.pojo.vo.ConfigVO;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统配置 控制器
 *
 * @author wz
 * @since 2024-08-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/config")
@Schema(description = "系统配置", name = "系统配置接口")
public class ConfigController {

	private final IConfigService configService;

	private final RedisTemplate<String, Object> redisTemplate;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Schema(description = "详情", example = "传入config")
	public Result<Config> detail(Config config) {
		Config detail = configService.getOne(Condition.getQueryWrapper(config));
		return Result.success(detail);
	}


	/**
	 * 自定义分页 系统配置
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Schema(description = "分页", example = "传入config")
	public Result<IPage<ConfigVO>> page(ConfigPageParams config, Query query) {
		IPage<ConfigVO> pages = configService.selectConfigPage(Condition.getPage(query), config);
		return Result.success(pages);
	}

	/**
	 * 新增 系统配置
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Schema(description = "新增", example = "传入config")
	public Result save(@Valid @RequestBody Config config) {
		config.setCreateTime(DateUtil.date());
		config.setCreateBy(Convert.toLong(StpUtil.getLoginId()));
		/**将值写入缓存中*/
		Map<String, String> map =new HashMap<>();
		map.put(config.getConfigKey(), config.getConfigValue());
		redisTemplate.opsForHash().putAll(RedisKeyConstants.SYSTEM_CONFIG_KEY, map);
//		EhcacheUtil.put(RedisKeyConstants.SYSTEM_CONFIG_KEY, RedisKeyConstants.CONFIG_KEY, map);

		return Result.judge(configService.save(config));
	}

	/**
	 * 修改 系统配置
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Schema(description = "修改", example = "传入config")
	public Result update(@Valid @RequestBody Config config) {
		config.setUpdateTime(DateUtil.date());
		config.setUpdateBy(Convert.toLong(StpUtil.getLoginId()));
		return Result.judge(configService.updateById(config));
	}

	/**
	 * 删除 系统配置
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@Schema(description = "删除", example = "传入ids")
	public Result remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return Result.judge(configService.removeByIds(Func.toLongList(ids)));
	}


	@Operation(summary = "刷新系统配置缓存")
	@GetMapping("/refreshCache")
	public Result refreshCache() {
		return Result.judge(configService.refreshCache());
	}


}
